//
//  CPImageUtil.swift
//  BabaLink
//
//  Created by wrs on 2018/8/1.
//  Copyright © 2018年 wrs. All rights reserved.
//

import Foundation
import CoreGraphics
import UIKit

public func getImage(color: UIColor) -> UIImage? {
    let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
    UIGraphicsBeginImageContext(rect.size)
    let context: CGContext? = UIGraphicsGetCurrentContext()
    context?.setFillColor(color.cgColor)
    context?.fill(rect)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}


extension UIImage {
    func resizableImageWithModeTile() -> UIImage {
        let size = self.size
        let leftEdge: CGFloat = (size.width - 1) * 0.5
        let topEdge: CGFloat = (size.height - 1) * 0.5
        let edgeInsets = UIEdgeInsets.init(top: topEdge, left: leftEdge, bottom: topEdge, right: leftEdge)
        let image = self.resizableImage(withCapInsets: edgeInsets, resizingMode: UIImage.ResizingMode.tile)
        return image
    }
    
    // 修复图片旋转
    func fixOrientation() -> UIImage {
           if self.imageOrientation == .up {
               return self
           }
           var transform = CGAffineTransform.identity
           
           switch self.imageOrientation {
           case .down, .downMirrored:
               transform = transform.translatedBy(x: self.size.width, y: self.size.height)
               transform = transform.rotated(by: .pi)
               break
               
           case .left, .leftMirrored:
               transform = transform.translatedBy(x: self.size.width, y: 0)
               transform = transform.rotated(by: .pi / 2)
               break
               
           case .right, .rightMirrored:
               transform = transform.translatedBy(x: 0, y: self.size.height)
               transform = transform.rotated(by: -.pi / 2)
               break
               
           default:
               break
           }
           
           switch self.imageOrientation {
           case .upMirrored, .downMirrored:
               transform = transform.translatedBy(x: self.size.width, y: 0)
               transform = transform.scaledBy(x: -1, y: 1)
               break
               
           case .leftMirrored, .rightMirrored:
               transform = transform.translatedBy(x: self.size.height, y: 0);
               transform = transform.scaledBy(x: -1, y: 1)
               break
               
           default:
               break
           }
           
           let ctx = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0, space: self.cgImage!.colorSpace!, bitmapInfo: self.cgImage!.bitmapInfo.rawValue)
           ctx?.concatenate(transform)
           
           switch self.imageOrientation {
           case .left, .leftMirrored, .right, .rightMirrored:
               ctx?.draw(self.cgImage!, in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(size.height), height: CGFloat(size.width)))
               break
               
           default:
               ctx?.draw(self.cgImage!, in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(size.width), height: CGFloat(size.height)))
               break
           }
           
           let cgimg: CGImage = (ctx?.makeImage())!
           let img = UIImage(cgImage: cgimg)
           
           return img
       }
}

enum ImageFormat {
    case Unknow
    case JPEG
    case PNG
    case GIF
    case TIFF
    case WebP
    case HEIC
    case HEIF
}
extension Data {
    func getImageFormat() -> ImageFormat  {
        var buffer = [UInt8](repeating: 0, count: 1)
        self.copyBytes(to: &buffer, count: 1)
        
        switch buffer {
        case [0xFF]: return .JPEG
        case [0x89]: return .PNG
        case [0x47]: return .GIF
        case [0x49],[0x4D]: return .TIFF
        case [0x52] where self.count >= 12:
            if let str = String(data: self[0...11], encoding: .ascii), str.hasPrefix("RIFF"), str.hasPrefix("WEBP") {
                return .WebP
            }
        case [0x00] where self.count >= 12:
            if let str = String(data: self[8...11], encoding: .ascii) {
                let HEICBitMaps = Set(["heic", "heis", "heix", "hevc", "hevx"])
                if HEICBitMaps.contains(str) {
                    return .HEIC
                }
                let HEIFBitMaps = Set(["mif1", "msf1"])
                if HEIFBitMaps.contains(str) {
                    return .HEIF
                }
            }
        default: break;
        }
        return .Unknow
    }
}
